#include "freesurroundboxactor.h"
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include "vectorproperties.h"
#include "numberproperty.h"
#include "stringproperty.h"
#include "transformable.h"
#include "datamanager.h"
#include <osg/BlendFunc>
#include <osg/BlendColor>
#include <osg/Depth>

#define DEFAULT_SIZE		1

namespace VrDataCore
{
	FreeSurroundBoxActor::FreeSurroundBoxActor()
	{
		setClassName(CLASS_FREESURROUNDBOX);
	}

	FreeSurroundBoxActor::~FreeSurroundBoxActor()
	{}

	void FreeSurroundBoxActor::buildPropertyMap()
	{
		addProperty(new StringProperty<DrawableActor>(XML_ELEMENT_DATATYPE_STRING,
			"Parent ID","",this,&DrawableActor::setParentID,
			&DrawableActor::getParentID,"",""));
		addProperty(new StringProperty<FreeSurroundBoxActor>(XML_ELEMENT_DATATYPE_STRING,
			"Vertex List","",this,&FreeSurroundBoxActor::setVertex,
			&FreeSurroundBoxActor::getVertex,"",""));
	}

	void FreeSurroundBoxActor::createDrawable()
	{
		_drawable = new CoreBase::Transformable(this);
		initialNode();
	}

	osg::ref_ptr<CoreBase::ActorBase> FreeSurroundBoxActor::clone()
	{
		osg::ref_ptr<CoreBase::ActorBase> clone = DrawableActor::clone();
		return clone;
	}

	void FreeSurroundBoxActor::initStateValue()
	{
		_ss = _geode->getOrCreateStateSet();
		osg::BlendFunc* bf = new osg::BlendFunc;
		osg::BlendColor* bc = new osg::BlendColor ;
		osg::ref_ptr<osg::Material> mat = new osg::Material;
		bf->setSource(osg::BlendFunc::CONSTANT_ALPHA);
		bf->setDestination(osg::BlendFunc::ONE_MINUS_CONSTANT_ALPHA);
		bc->setConstantColor(osg::Vec4(1.0f,1.0f,0.0f,0.4f));
		mat->setColorMode(osg::Material::ColorMode::OFF);
		mat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
		mat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
		mat->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
		mat->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0f,1.0f,0.0f,0.4f));
		_ss->setAttributeAndModes(bc, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
		_ss->setAttributeAndModes(mat,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
		_ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); 
		_ss->setAttributeAndModes(bf, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
		osg::ref_ptr<osg::Program> program = new osg::Program;
		program->setName("none program");
		_ss->setAttributeAndModes( program.get(), osg::StateAttribute::ON );
	}

	void FreeSurroundBoxActor::setVertex(QString vertex)
	{
		QStringList vertexlist = vertex.split(",",QString::SkipEmptyParts);
		if(vertexlist.size() == 2)
		{
			_vertexString = vertex;
			QStringList ls = vertexlist.at(0).split(" ",QString::SkipEmptyParts);
			_minVertex.x() = ls.at(0).toFloat();
			_minVertex.y() = ls.at(1).toFloat();
			_minVertex.z() = ls.at(2).toFloat();
			ls = vertexlist.at(1).split(" ",QString::SkipEmptyParts);
			_maxVertex.x() = ls.at(0).toFloat();
			_maxVertex.y() = ls.at(1).toFloat();
			_maxVertex.z() = ls.at(2).toFloat();
		}
	}

	QString FreeSurroundBoxActor::getVertex()
	{
		_vertexString = "";
		_vertexString = QString::number(_minVertex.x())+" "+QString::number(_minVertex.y())+" "+QString::number(_minVertex.z());
		_vertexString = _vertexString + "," + QString::number(_maxVertex.x())+" "+ 
			QString::number(_maxVertex.y())+" "+QString::number(_maxVertex.z());
		return _vertexString;
	}

	void FreeSurroundBoxActor::setVertex(const osg::Vec3&  minvec3,const osg::Vec3& maxvec3)
	{
		_minVertex = minvec3;
		_maxVertex = maxvec3;
		osg::Vec3 size;
		size.x() = _maxVertex.x() - _minVertex.x();
		size.y() = _maxVertex.y() - _minVertex.y();
		size.z() = _maxVertex.z() - _minVertex.z();
		osg::Vec3 pos;
		pos = (_maxVertex+_minVertex)/2;
		_transformNode->setScale(size);
		_transformNode->setPosition(pos);
	}

	void FreeSurroundBoxActor::getVertex(osg::Vec3&  minvec3,osg::Vec3& maxvec3)
	{
		minvec3 = _minVertex;
		maxvec3 = _maxVertex;
	}

	void FreeSurroundBoxActor::onMapLoadEnd(CoreBase::Map* map)
	{
		setVertex(_minVertex,_maxVertex);
	}

	void FreeSurroundBoxActor::initialNode()
	{
		osg::Vec3 size = osg::Vec3(DEFAULT_SIZE,DEFAULT_SIZE,DEFAULT_SIZE);
		_pBox = new osg::Box( osg::Vec3(0,0,0), size.x(),size.y(),size.z());
		_shapeDrawable = new osg::ShapeDrawable(_pBox.get());
		_geode = new osg::Geode();
		_geode->addDrawable(_shapeDrawable.get());
		_transformNode = new osg::PositionAttitudeTransform;
		_transformNode->addChild(_geode);
		this->getDrawable()->getOSGNode()->asGroup()->addChild(_transformNode.get());
		initStateValue();
		setTraverseMask(NODE_MASK_NOSELECT);
	}
}